package com.guigu.qingcheng.systemservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.pojo.system.TbAdmin;
import com.guigu.qingcheng.pojo.system.TbAdminRoleList;
import com.guigu.qingcheng.systemservice.service.TbAdminService;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/systemservice/tb-admin")
public class TbAdminController {
    @Autowired
    private TbAdminService tbAdminService;

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping("/admin/findAll")
    public R findAll(){
        List<TbAdmin> list = tbAdminService.list(null);
        if(list.size() <= 0)
            return R.error().data("items", list);
        return R.ok().data("items", list);
    }

    /**
     * 分页查询数据
     * @param page 页号
     * @param size 页大小
     * @return
     */
    @GetMapping("/admin/findPage")
    public R findPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size){
        Page<TbAdmin> pg = new Page<>(page, size);
        IPage<TbAdmin> list = tbAdminService.page(pg, null);
        if(pg.getRecords().size() <= 0)
            return R.error().data("total", pg.getTotal()).data("pgitems", pg.getRecords());
        return R.ok().data("total", pg.getTotal()).data("pgitems", pg.getRecords());
    }

    /**
     * 条件查询数据
     * @param searchMap, 查询所带条件
     * @return
     */
    @PostMapping("/admin/findList")
    public R findList(@RequestBody Map<String, Object> searchMap){
        List<TbAdmin> list = tbAdminService.findList(searchMap);
        if(list.size() <= 0)
            return R.error().data("listItems", list);
        return R.ok().data("listItems", list);
    }

    /**
     * 分页+条件查询
     * @param page 页号
     * @param size 页大小
     * @param searchMap 条件
     * @return
     */
    @PostMapping("/admin/findPage")
    public R findPage(@RequestBody(required=false) Map<String, Object> searchMap, Integer page, Integer size){
        Page<TbAdmin> pg = tbAdminService.findPage(page, size, searchMap);
        if(pg.getRecords().size() <= 0)
            return R.error().data("total", pg.getTotal()).data("fpgitems", pg.getRecords());
        return R.ok().data("total", pg.getTotal()).data("fpgitems", pg.getRecords());
    }

    /**
     * 根据id查询数据
     * @param id 所传id
     * @return
     */
    @GetMapping("/admin/findById")
    public R findById(@RequestParam("id") Integer id){
        TbAdmin tbAdmin = tbAdminService.getById(id);
        if(tbAdmin == null)
            return R.error().data("pojo", tbAdmin);
        return R.ok().data("pojo", tbAdmin);
    }

    /**
     * 增加
     * @param admin 增加的信息
     * @return
     */
        @PostMapping("/admin/add")
    public R add(@RequestBody TbAdmin admin){
       Boolean f = tbAdminService.save(admin);
       if(f == false)
           return R.error();
       return R.ok();
    }

    /**
     * 修改
     * @param admin
     * @return
     */
    @PostMapping("/admin/update")
    public R update(@RequestBody TbAdmin admin){
        boolean f = tbAdminService.updateById(admin);
        if(f == false)
            return R.error();
        return R.ok();
    }

    @PostMapping("/admin/updateUser")
    public R updateUser(@RequestBody Map<String, Object> zhhuForm) {
        System.out.println(zhhuForm);
        R f = tbAdminService.updateByUser(zhhuForm);
        return f;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/admin/delete")
    public R delete(@RequestParam("id") Integer id){
        Boolean f = tbAdminService.removeById(id);
        if(f == false)
            return R.error();
        return R.ok();
    }

    /**
     * 查询所有成员
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/admin/findPage2")
    public R findPage2(@RequestBody(required=false) Map<String, Object> searchMap, Integer page, Integer size){
        List<TbAdminRoleList> pg = tbAdminService.findPage2(page, size, searchMap);
        if(pg.size() <= 0)
            return R.error().data("total", pg.size()).data("list", pg);
        return R.ok().data("total", pg.size()).data("list", pg);
    }

    /**
     * 添加成员
     * @param adminRoleForm
     * @return
     */
    @PostMapping("/admin/save")
    public R save(@RequestBody(required = false) Map<String, Object> adminRoleForm){
        Boolean f = tbAdminService.addSave(adminRoleForm);
        if(f)
            return R.ok();
        return R.error();
    }

    /**
     * 修改成员
     * @param adminRoleForm
     * @return
     */
    @PostMapping("/admin/updateAdmin")
    public R updateAdmin(@RequestBody(required = false) Map<String, Object> adminRoleForm){
        Boolean f = tbAdminService.updateAdmin(adminRoleForm);
        if(f)
            return R.ok();
        return R.error();
    }

    /**
     * 修改成员状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/admin/updateStatus")
    public R updateStatus(Integer id, String status){
        Boolean f = tbAdminService.updateStatus(id, status);
        if(f)
            return R.ok();
        return R.error();
    }

}

